package com.cool.cloud.controller;

import com.cool.cloud.entity.User;
import com.cool.cloud.service.*;
import com.cool.cloud.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: BaseController
 * @Description: 控制器的基类，所有控制器必须继承此类
 * @author: laijunji
 * @date 2020/2/25 18:19
 * @Version: 1.0
 **/
public class BaseController {

    @Autowired
    protected UserService userService;
    @Autowired
    protected MyFileService myFileService;
    @Autowired
    protected FileFolderService fileFolderService;
    @Autowired
    protected FileStoreService fileStoreService;
    @Autowired
    protected TempFileService tempFileService;
    @Autowired
    protected ShareFileService shareFileService;
    @Autowired
    protected FileWeekCountService fileWeekCountService;
    @Autowired
    protected BGWorkService bgWorkService;
    @Autowired
    protected SizeTypeService sizeTypeService;
    @Autowired
    protected SliderPictureService sliderPictureService;

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    protected User loginUser;

    @Autowired
    protected JavaMailSenderImpl mailSender;
    protected MailUtils mailUtils;

    /**
     * 在每个子类方法调用之前先调用
     * 设置request,response,session这三个对象
     *
     * @param request
     * @param response
     */
    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession(true);
        loginUser = (User) session.getAttribute("loginUser");
    }

    /**
     * 返回指定页面
     *
     * @param page
     * @return String
     */
    protected String returnPage(String page, int cur, String searchName, Integer fId, String fName) {
        if (page.equals("video")) {
            return "redirect:/file-video?cur=" + cur;
        } else if (page.equals("music")) {
            return "redirect:/file-music?cur=" + cur;
        } else if (page.equals("doc")) {
            return "redirect:/file-doc?cur=" + cur;
        } else if (page.equals("image")) {
            return "redirect:/file-image?cur=" + cur;
        } else if (page.equals("other")) {
            return "redirect:/file-other?cur=" + cur;
        } else if (page.equals("file-all")) {
            String url = "redirect:/file-all?";
            if (fName != null) {
                url += "&fName=" + fName;
            }
            if (fId != null) {
                url += "&fId=" + fId;
            }
            return url + "&cur=" + cur;
        } else if (page.equals("search")) {
            return "redirect:/search?fileName=" + searchName + "&cur=" + cur;
        } else
            return "未完待续";

    }
}
